package gitapp.oschina.net.my_osc.api;

import android.os.Build;
import android.text.TextUtils;

import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;

import gitapp.oschina.net.my_osc.AppContext;
import gitapp.oschina.net.my_osc.common.CyptoUtils;

/**
 * Created by houjianhua on 2016/10/25.
 */

public class AsyncHttpHelp {
    public final static String PRIVATE_TOKEN = "private_token";
    public final static String GITOSC_PRIVATE_TOKEN = "git@osc_token";

    public static void get(String url, HttpCallback handler){
        RxVolley.get(url, handler);
    }

    public static void get(String url, HttpParams params, HttpCallback handler){
        new RxVolley.Builder().shouldCache(false).url(url).params(params).callback(handler).doTask();
    }

    public static void post(String url, HttpParams params, HttpCallback handler){
        RxVolley.post(url, params, handler);
    }

    public static String getUserAgent(){
        AppContext appContext = AppContext.getInstance();
        StringBuilder ua = new StringBuilder("Git@OSC.NET");
        ua.append('/' + appContext.getPackageInfo().versionName + '_' + appContext.getPackageInfo().versionCode);
        ua.append("/Android");
        ua.append("/" + Build.VERSION.RELEASE);
        ua.append("/" + Build.MODEL);
        ua.append("/" + AppContext.getInstance().getAppId());
        return ua.toString();
    }

    public static HttpParams getPrivateTokenWithParams(){
        HttpParams params = new HttpParams();
        params.putHeaders("User-Agent", getUserAgent());
        String private_token = AppContext.getInstance().getProperty(PRIVATE_TOKEN);
        private_token = CyptoUtils.decode(GITOSC_PRIVATE_TOKEN, private_token);
        if (!TextUtils.isEmpty(private_token)){
            params.put(PRIVATE_TOKEN, private_token);
        }
        return params;
    }

}
